#pragma once

#include <iostream>
#include <string>
#include <vector>
#include <ctemplate/template.h>
#include "oj_model.hpp"

namespace ns_view
{
    using namespace ns_model;
    using namespace ctemplate;

    const std::string template_path = "./template_html/";

    class View
    {
    public:
        View() {}

        // 将所有题目渲染为题单html
        void AllQuestionsExpandHtml(const std::vector<Question> &questions, std::string *html)
        {
            // 构建模板html路径src_html
            std::string src_html = template_path + "question_list.html";

            // 构造数据字典all_questions
            ctemplate::TemplateDictionary root("all_questions");

            // 循环遍历所有题目信息
            for (const auto &q : questions)
            {
                // 构造子字典，并添加到question_list
                ctemplate::TemplateDictionary *sub = root.AddSectionDictionary("question_list");

                // 设置子字典的key和value
                sub->SetValue("number", q.number);
                sub->SetValue("title", q.title);
                sub->SetValue("level", q.level);
            }
            // 获取准备被渲染的html
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src_html, ctemplate::DO_NOT_STRIP);

            // 将数据渲染到html中
            tpl->Expand(html, &root);
        }

        // 将单个题目渲染为题目html
        void OneQuestionExpandHtml(const Question &q, std::string *html)
        {
            // 构建模板html路径src_html
            std::string src_html = template_path + "question_detail.html";

            // 构造数据字典one_question
            ctemplate::TemplateDictionary root("one_question");
            root.SetValue("number", q.number);
            root.SetValue("title", q.title);
            root.SetValue("level", q.level);
            root.SetValue("desc", q.desc);
            root.SetValue("pre_code", q.header);

            // 获取准备被渲染的html
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src_html, ctemplate::DO_NOT_STRIP);

            // 将数据渲染到html中
            tpl->Expand(html, &root);
        }

        ~View() {}
    };
}